package com.pearlsoft.webapp.rms.vdcmgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pearlsoft.rms.VcloudManager;
import com.pearlsoft.webapp.core.annotation.QueryHandlerComponent;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Query;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.dataset.SimpleRecord;
import com.pearlsoft.webapp.core.web.basehandler.AbsBaseQueryHandler;
import com.vmware.vcloud.api.rest.schema.CapacityWithUsageType;
import com.vmware.vcloud.api.rest.schema.ComputeCapacityType;
import com.vmware.vcloud.api.rest.schema.VdcType;
import com.vmware.vcloud.sdk.Organization;
import com.vmware.vcloud.sdk.Vdc;

@QueryHandlerComponent(name = "VDCMgrQueryHandler")
public class VDCMgrQueryHandler extends AbsBaseQueryHandler {

	private Record changeVdcToRecord(Vdc v, VdcType vtype) {
		CapacityWithUsageType ctype = vtype.getStorageCapacity();
		ComputeCapacityType cctype = vtype.getComputeCapacity();
		CapacityWithUsageType cpu = cctype.getCpu();
		CapacityWithUsageType mem = cctype.getMemory();
		SimpleRecord recrod = new SimpleRecord();
		recrod.setValue("VDC_Name", vtype.getName());
		recrod.setValue("VDC_Desc", vtype.getDescription());
		recrod.setValue("VDC_Ststus", vtype.getStatus().toString());
		recrod.setValue("VDC_Enable", vtype.isIsEnabled());
		recrod.setValue("VDC_CpuLimit", cpu.getLimit());
		recrod.setValue("VDC_CpuUsed", cpu.getUsed());
		recrod.setValue("VDC_CpuAssign", cpu.getAllocated());
		recrod.setValue("VDC_CpuOverhead", cpu.getOverhead());
		recrod.setValue("VDC_CpuUnits", cpu.getUnits());
		recrod.setValue("VDC_MemoryLimit", mem.getLimit());
		recrod.setValue("VDC_MemoryUsed", mem.getUsed());
		recrod.setValue("VDC_MemoryAssign", mem.getAllocated());
		recrod.setValue("VDC_MemoryOverhead", mem.getOverhead());
		recrod.setValue("VDC_MemoryUnits",mem.getUnits());
		recrod.setValue("VDC_MaxVMNum", vtype.getVmQuota().toString());	
		recrod.setValue("VDC_HardDiskUsed", ctype.getUsed());
		recrod.setValue("VDC_HardDiskAssign", ctype.getAllocated());
		recrod.setValue("VDC_HardDiskLimit", ctype.getLimit());
		recrod.setValue("VDC_HardDiskOverHead", ctype.getOverhead());
		recrod.setValue("VDC_HardDiskUnits",ctype.getUnits());
		recrod.setValue("VDC_MaxNetNum", vtype.getNetworkQuota());
		return recrod;
	}

	@Override
	public void advSearch(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub
		String vDC_Name = (String) query.get("VDC_Name");
		VcloudManager vcm = this.getBean("vcloudManager",
				VcloudManager.class);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		List<Record> list = new ArrayList<Record>(); 
		for (Map.Entry<String, Vdc> entry : vdcs.entrySet()) {
			Vdc v = entry.getValue();
			VdcType vtype = v.getResource();
			if (vtype.getDescription().indexOf(vDC_Name)!=-1)
				list.add(changeVdcToRecord(v, vtype));
		}
		ds.setRecords(list);

	}

	@Override
	public void getEntity(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void quickSearch(Query query, DataSet ds) throws Exception {
		String searchValue = (String) query.get("searchValue");
		VcloudManager vcm = this.getBean("vcloudManager",
				VcloudManager.class);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		List<Record> list = new ArrayList<Record>();
		for (Map.Entry<String, Vdc> entry : vdcs.entrySet()) {
			Vdc v = entry.getValue();
			VdcType vtype = v.getResource();
			if (vtype.getDescription().indexOf(searchValue)!=-1)
				list.add(changeVdcToRecord(v, vtype));
		}
		ds.setRecords(list);

	}

	@Override
	public void handler(Query query, DataSet ds) throws Exception {
		VcloudManager vcm = this.getBean("vcloudManager",
				VcloudManager.class);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		List<Record> list = new ArrayList<Record>();
		for (Map.Entry<String, Vdc> entry : vdcs.entrySet()) {
			Vdc v = entry.getValue();
			VdcType vtype = v.getResource();
			list.add(changeVdcToRecord(v, vtype));
		}
		ds.setRecords(list);
	}
}
